package com.hyg.lib_music.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_common.DataModel.Music.MusicPreSleepBehavior;
import com.hyg.lib_music.R;
import com.makeramen.roundedimageview.RoundedImageView;

import me.drakeet.multitype.ItemViewBinder;

public class MusicPreSleepBehaviorViewBinder extends ItemViewBinder<MusicPreSleepBehavior, MusicPreSleepBehaviorViewBinder.ViewHolder> {

    private OnMusicPreSleepBehaviorClickListener onMusicPreSleepBehaviorClickListener;

    @Override
    protected @NonNull
    ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_music_sleep_pre, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull MusicPreSleepBehavior musicPreSleepBehavior) {
        holder.setData(musicPreSleepBehavior);
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public MusicPreSleepBehavior musicPreSleepBehavior;
        private @NonNull
        LinearLayout sleep_pre;
        private @NonNull
        TextView name;
        private @NonNull
        RoundedImageView ico;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            sleep_pre = itemView.findViewById(R.id.sleep_pre);
            name = itemView.findViewById(R.id.name);
            ico = itemView.findViewById(R.id.ico);
            sleep_pre.setOnClickListener(this);
        }

        void setData(@NonNull final MusicPreSleepBehavior musicPreSleepBehavior) {
            this.musicPreSleepBehavior = musicPreSleepBehavior;
            if (musicPreSleepBehavior.isSelected){
                ico.setImageResource(musicPreSleepBehavior.selectedIco);
                ico.setBackgroundColor(Color.parseColor("#FFFFFF"));
                name.setTextColor(Color.parseColor("#7957a9"));
            } else {
                ico.setImageResource(musicPreSleepBehavior.ico);
                ico.setBackgroundColor(Color.parseColor("#EDEDED"));
                name.setTextColor(Color.parseColor("#333333"));
            }
            name.setText(musicPreSleepBehavior.name);
        }

        @Override
        public void onClick(View view) {
            if (onMusicPreSleepBehaviorClickListener != null) {
                if (view.getId() == R.id.sleep_pre) {
                    onMusicPreSleepBehaviorClickListener.onItemClick(view, this.musicPreSleepBehavior);
                }
            }
        }
    }

    // 自定义点击事件
    public void setOnMusicPreSleepBehaviorClickListener(OnMusicPreSleepBehaviorClickListener onMusicPreSleepBehaviorClickListener) {
        this.onMusicPreSleepBehaviorClickListener = onMusicPreSleepBehaviorClickListener;
    }

    public interface OnMusicPreSleepBehaviorClickListener {
        void onItemClick(View view, MusicPreSleepBehavior musicPreSleepBehavior);
    }
}